<?php
$refPage = "register.php";
include( "langincluder.php" );

$step = (int)$_GET['st'];

if( isset( $_SESSION['userName'] ) && !empty( $_SESSION['userName'] ) )
	$step = 4;

switch( $step )
{
	case 0 : // regiser gui
?>

<script type="text/javascript">
var redBGColor = "#ffaaaa";
var greenBGColor = "#aaffaa";
var defaultColor = "unset";

function makeRed(e)
{
	if( defaultColor == "unset" ) defaultColor = e.style.backgroundColor;
	e.style.backgroundColor = redBGColor;
}
function makeDef(e)
{
	e.style.backgroundColor = defaultColor;
}
function makeGreen(e)
{
	e.style.backgroundColor = greenBGColor;
}

var errors = new Array();
errors[2] = new Array(<?php echo "'".$strings['ajaxErrorUsernameFormatLength']."', '".$strings['ajaxErrorUsernameFormatBadChars']."', '".$strings['ajaxErrorUsernameInUse']."'";?>); // username errors
errors[3] = new Array(<?php echo "'".$strings['ajaxErrorPasswordFormatLength']."', '".$strings['ajaxErrorPasswordMismatch']."'";?>); // password errors
errors[1] = new Array(<?php echo "'".$strings['ajaxErrorEmailFormat']."', '".$strings['ajaxErrorEmailInUse']."'";?>); // email errors


var checkFlag;
var isok = new Array( false, false, false );

function checkForm(num)
{
	var ajax;
	if (window.XMLHttpRequest)
		ajax = new XMLHttpRequest();
	else
		ajax = new ActiveXObject("Microsoft.XMLHTTP");
	
	var elem;
	switch( num )
	{
		case 1 : elem = "mailInp"; break;
		case 2 : elem = "userNameInp"; break;
		case 3 : elem = "passwordInp"; break;
		default : return;
	}
	
	if( num == 3 )
	{
		if( document.getElementById("passwordInp").value == document.getElementById("passwordInp2").value ) {}
		else 
		{
			makeRed( document.getElementById("passwordInp") );
			makeRed( document.getElementById("passwordInp2") );
			document.getElementById("passwordInpInfo").innerHTML = errors[3][1];
			return 1;
		}
	}
	
	var cont = "data=" + document.getElementById(elem).value;
	ajax.open("POST", "./account/checker.php?ajax=1&check="+num, true);
	ajax.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	ajax.send(cont);
	
	var ise = 0;
	ajax.onreadystatechange = function()
	{
		if (ajax.readyState==4 && ajax.status==200)
		{
			if( ajax.responseText == '0' ) 
			{
				document.getElementById(elem + "Info").innerHTML = "OK";
				
				makeGreen( document.getElementById(elem) );
				if( num == 3 ) makeGreen( document.getElementById("passwordInp2") );
				isok[num-1] = true;
				checkFlag--;
			}
			else
			{
				document.getElementById(elem + "Info").innerHTML = errors[num][ajax.responseText-1];
				
				makeRed( document.getElementById(elem) );
				if( num == 3 ) makeRed( document.getElementById("passwordInp2") );
				isok[num-1] = false;
				checkFlag--;
			}
		}
	}
}	

function de()
{
	if(checkFlag==0)
	{
		if( isok[0] && isok[1] && isok[2] )
		{
			document.forms["regUserForm"].submit();
			//alert("WIN!");
		}
		else
		{
			//alert("ERRRR!!!");
		}
	}
	else
		setTimeout("de()", 100);
}
function submiter()
{
	checkFlag = 3;
	checkForm(1); checkForm(2); checkForm(3);
	de();
}
</script>

<style type="text/css">
td
{
	padding : 5px 3px 5px 0px;
}
</style>

<div>
<table>
<form id="regUserForm" method="POST" action="./?p=201&st=2">
<tr>
<td><?php echo $strings['usernameLabel']; ?>
	<div class="additionalInfo"><?php echo $strings['usernameAddInfo']; ?></div></td>
<td>
<input name="usernR" id="userNameInp" type='text'>
<button type="button" onclick="checkForm(2)"><?php echo $strings['checkButtonLabel']; ?></button>
<div id="userNameInpInfo"></div>
</td>
</tr>

<tr>
<td><?php echo $strings['passwordLabel']; ?>
	<div class="additionalInfo"><?php echo $strings['passwordAddInfo']; ?></div></td>
<td>
<input name="passwR" id="passwordInp" type='password'>
</td>
</tr>
<tr>
<td><?php echo $strings['confirmPasswordLabel']; ?></td>
<td>
<input name="passwR2" id="passwordInp2" type='password'>
<button type="button" onclick="checkForm(3)"><?php echo $strings['checkButtonLabel']; ?></button>
<div id="passwordInpInfo"></div>
</td>
</tr>

<tr>
<td><?php echo $strings['emailLabel']; ?>
	<div class="additionalInfo"><?php echo $strings['emailAddInfo']; ?></div></td>
<td>
<input name="mailR" id="mailInp" type='text'>
<button type="button" onclick="checkForm(1)"><?php echo $strings['checkButtonLabel']; ?></button>
<div id="mailInpInfo"></div>
</td>
</tr>

<tr>
<td><?php echo $strings['captchaLabel']; ?></td>
<td>
<img src='captcha/securimage_show.php' id='si'>
<br>
<input name="captchaR" id="captchaInp" type='text'>
<br>
<a onclick="document.getElementById('si').src = 'captcha/securimage_show.php?sid='+ Math.random();">
<button type="button"><?php echo $strings['captchaReloadButtonLabel']; ?></button>
</a>
</td>
</tr>
</form>
<tr>
<td colspan="2" style="text-align : center;">
<button type="button" onclick="submiter()" style="width: 200px; height:40px;"> <?php echo $strings['submitButtonLabel']; ?> </button>
</td>
</tr>
</table>
</div>

<?php
		break;
	case 2 : // place the record into sql
		// captcha check
		include("./captcha/securimage.php");
		$img = new Securimage();
		if( !($img->check($_POST['captchaR'])) )
		{
			echo $strings['captchaFail'].'<br><button type="button" onclick="history.back()">'.$strings['goBack'].'</button>';
			break;
		}
		
		$omgHax = "if you see this message then you have just failed badly at hacking the ";
		
		// recheck the submited stuff
		include( "./account/checker.php" );
		$usern = $_POST['usernR'];
		if( usernameCheck($usern) )
		{
			echo $omgHax."username check";
			break;
		}
		$passw = $_POST['passwR'];
		if( $passw != $_POST['passwR2'] )
		{
			break;
		}
		if( passwordCheck($passw) )
		{
			echo $omgHax."password check";
			break;
		}
		
		$mail = $_POST['mailR'];
		if( mailCheck($mail) )
		{
			echo $omgHax."e-mail check";
			break;
		}
		$passw = sha1($passw);
		$q = mysql_query("SELECT userId FROM $usersTableName ORDER BY userId DESC LIMIT 1");
		$r = mysql_fetch_array($q);
		$uidn = $r['userId']+1;
		
		mysql_query("INSERT INTO $usersTableName (userId, userName, sha1Pass, inactive) 
                     VALUES ('$uidn', '$usern', '$passw', '".substr(sha1(rand()), 0, 14)."')");
		
		require("./uploader/uploaderConfigs.php");
		
		mysql_query("INSERT INTO $usersExtendedTableName (userId, email, regOn, userLevel, warn, lang, 
															uploadMaxFileSizeKB, uploadGrantedSpaceMB, uploadUsedSpaceKB, uploadAllowedTypes)  
                     VALUES ('$uidn', '$mail', '".date('j/n/Y, H:i')."', 3, 0, 'en', 
								$defaultMaxFileSizeKB, $defaultGrantedSpaceMB, 0, '$defaultAllowedTypes')");
		
		if( mysql_error() ){ echo mysql_error(); break; }
		echo $strings['accountCreated']."<br>( $usern, $mail )";
		break;
		case 4: echo 'o hai!'; break;
}

?>

